<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:34
 */

namespace app\service;


use app\model\CartModel;
use think\Exception;

/**
 * 购物车
 * Class CartService
 * @package app\service
 */
class CartService extends BaseService
{
    public $cartModel;
    public function __construct()
    {
        $this->cartModel=new CartModel();
    }


    /**
     * 查询商品列表
     * @param $where
     * @return array|bool
     */
    public function queryList($where){
        try{
           return $this->cartModel
               ->field('
               zkhb_cart.*,
               zkhb_goods.name,
               zkhb_goods.price,
               zkhb_goods.shop_price,
               zkhb_goods.picture,
               zkhb_goods.buy_min,
               zkhb_goods.buy_max,
               zkhb_goods.buy_step,
               zkhb_goods.shop_type
               ')
               ->join('zkhb_goods','zkhb_cart.goods_id=zkhb_goods.id')
               ->where($where)->select()->toArray();
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 查询商品列表
     * @param $where
     * @return array|bool
     */
    public function queryCartGoodsList($where){
        try{
            return $this->cartModel
                ->field('
               zkhb_goods.*,
               zkhb_cart.goods_num,
               zkhb_cart.sku_id
               ')
                ->join('zkhb_goods','zkhb_cart.goods_id=zkhb_goods.id')
                ->where($where)->select()->toArray();
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }


    /**
     * 添加购物车
     * @param $add
     * @return bool|false|int
     */
    public function addCart($add){
        try{
           return $this->cartModel->insertGetId($add);
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 购物车商品数量
     * @param $id
     * @param $goods_num
     * @param string $type
     * @return bool|int|true
     */
    public function descCart($id,$goods_num,$type='desc'){
        try{
            if($type=='desc'){
               return $this->cartModel->where(array('id'=>$id))->setInc('goods_num',$goods_num);
            }else{
               return $this->cartModel->where(array('id'=>$id))->setDec('goods_num',$goods_num);
            }

        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 购物车商品数量
     * @param $id
     * @param $save
     * @param string $type
     * @return bool|int|true
     */
    public function saveCart($save,$id=null){
        try{
            if($id){
                return $this->cartModel->save($save,array('id'=>$id));
            }else{
                return $this->cartModel->insertGetId($save);
            }

        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**删除购物车
     * @param $ids
     * @param $uid
     * @return int
     */
    public function delCart($ids,$uid){
        $where['id']=array('in',$ids);
        $where['uid']=$uid;
        return $this->cartModel->where($where)->delete();
    }

    public function countCart($uid){
        try{
            return $this->cartModel->where(array('uid'=>$uid))->count();
        }catch (Exception $ex){
            LogService::S()->writeLog('GoodsService','Exception-delCart',$ex->getMessage());
            return false;
        }

    }
}